package homework.demo0604.controller;


import homework.demo0604.mapper.IUserMapper;
import homework.demo0604.pojo.UserInfo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Scanner;

@Component
public class UserController {
    private static final Log log = LogFactory.getLog(UserController.class);
    @Autowired
    private IUserMapper userMapper;
    public void start(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入序号");
        System.out.println("1:用户注册");
        System.out.println("2:用户登录");
        System.out.println("3:修改密码");
        System.out.println("4:用户列表");
        int i = scanner.nextInt();
        switch (i){
            case 1:reg();
            break;
            case 2:login();
            break;
        }
    }
    public void reg(){
        System.out.println("UserMapper:开始用户注册...");
        System.out.println("欢迎注册");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = scanner.nextLine();
        System.out.println("请输入密码:");
        String password = scanner.nextLine();
        System.out.println("请输入昵称:");
        String nickname = scanner.nextLine();
        System.out.println("请输入年龄:");
        int age = scanner.nextInt();
        UserInfo info = new UserInfo(username,password,nickname,age);
        int num = userMapper.insert(info);
        System.out.println(num>0?"注册成功":"注册失败");
    }
    public void login(){

        System.out.println("UserMapper:开始用户登录...");
        System.out.println("欢迎注册");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = scanner.nextLine();
        System.out.println("请输入密码:");
        String password = scanner.nextLine();
        UserInfo userInfo = userMapper.select(username);
        if (userInfo != null) {
            if (userInfo.getPassword().equals(password)) {
                System.out.println("登陆成功");
            }else {
                System.out.println("密码错误");
            }
        }else{
            System.out.println("用户不存在");
        }

    }
}
